import QtQuick 2.0

//HOME:上划立即松开
//TASK:上划停顿
MouseArea{
    id: root
    property vector2d pressedVec                        //按下点
    property vector2d distanceVec                       //移动距离
    property int moveThreshold: 100                     //滑动距离触发阈值
    property alias holdInterval: timer.interval         //触发多任务的停留时间

    property real bottomPadding: 4                      //指示器距离父容器的底部距离
    property real activityHeight: height * 1.0          //指示器的活动高度 可移动的高度
    property alias color: indicator.color               //指示器颜色
    property color borderColor: "white"                 //指示器边框色
    property int borderWidth: 0                         //指示器边框宽度
    property alias indicatorHeight: indicator.height    //指示器高度

    signal homeTriggered()                              //触发home键
    signal multitaskTriggered()                         //触发多任务键

    QtObject{
        id: data
        property bool triggered: false                  //标记本次按下是否已经触发信号 防止重复触发
    }

    onPressed: {
        data.triggered = false
        pressedVec = Qt.vector2d(mouseX,mouseY)
    }
    onReleased: {
        //检查是否超出阈值 并且定时器还在运行(task未触发过)
        if((Math.abs(distanceVec.y) > moveThreshold) && timer.running)
            homeTriggered();

        pressedVec = Qt.vector2d(0,0)
        distanceVec = Qt.vector2d(0,0)

        timer.stop();
    }
    onPositionChanged:{
        var posVec = Qt.vector2d(mouseX,mouseY)
        //计算两点距离
        distanceVec = pressedVec.minus(posVec);

        if(!data.triggered)
            timer.start()
    }

    Timer{
        id: timer
        interval: 400
        onTriggered: {
            console.log("timeout:",root.pressed)
            if(root.pressed && (distanceVec.y > moveThreshold)){
                console.log("TASK:")
                multitaskTriggered();
                data.triggered = true;
            }
        }
    }
    Rectangle{
        anchors.fill: parent
        color: "red"
    }

    //指示器
    Rectangle{
        id: indicator
        y: {
            //计算移动比例 0.0~1.0
            var offsetRatio = distanceVec.y / moveThreshold;
            if(offsetRatio > 1) offsetRatio = 1;
            if(offsetRatio < 0) offsetRatio = 0;
            return (parent.height-height-bottomPadding) - offsetRatio * (activityHeight-height-bottomPadding);
        }
        Behavior on y {NumberAnimation{duration: 100}}
        height: 4
        width: parent.width / 2
        anchors.horizontalCenter: parent.horizontalCenter
        radius: height / 2
        border.color: borderColor
        border.width: borderWidth
    }
}
